var data;
//获取待处理事务信息
getPendingTransactions();
//查询用户数量
getNums();
//查询本月课程新增总数
getNowMonthCourseNums('getNowMonthCourseNums','本月新增课程总数');
//获取echarts数据
getDataEcharts("getCourseDay");

getChangeDateEcharts('course');
function getChangeDateEcharts(tabName) {
    if (tabName == 'course'){
        $('.today').addClass('borderBottom');
        $('.week').removeClass('borderBottom');
        $('.month').removeClass('borderBottom');
        //查询本月课程新增总数
        getNowMonthCourseNums('getNowMonthCourseNums','本月新增课程总数');
        getDataEcharts("getCourseDay","课程统计","课程量");
    }else if (tabName == 'product'){
        $('.week').addClass('borderBottom');
        $('.today').removeClass('borderBottom');
        $('.month').removeClass('borderBottom');
        getNowMonthCourseNums('getNowMonthProductNums','本月新增商品总数');
        getDataEcharts("getProductDay","商品统计","商品量");
    }else if (tabName == 'before_user'){
        $('.month').addClass('borderBottom');
        $('.week').removeClass('borderBottom');
        $('.today').removeClass('borderBottom');
        getNowMonthCourseNums('getNowMonthUserNums','本月新增用户总数');
        getDataEcharts("getBeforeUserDay","用户统计","用户量");
    }
}

function getNowMonthCourseNums(request, monthTypeName) {
    $.ajax({
        url: '/systemIndex/' + request,
        type: 'post',
        dataType:'json',
        data: {},
        success:function (data) {
            if (request == "getNowMonthCourseNums") {
                $('#monthTypeName').text(monthTypeName);
                $('#monthSum').text(data.data.nowMonthCourseNums);
                var monthSumRate = Math.round((data.data.nowMonthCourseNums - data.data.lastMonthCourseNums) / data.data.lastMonthCourseNums * 100);
                $('#monthSumRate').text(monthSumRate + "%");
            }else if (request == "getNowMonthProductNums"){
                $('#monthTypeName').text(monthTypeName);
                $('#monthSum').text(data.data.nowMonthProductNums);
                var monthSumRate = Math.round((data.data.nowMonthProductNums - data.data.lastMonthProductNums) / data.data.lastMonthProductNums * 100);
                $('#monthSumRate').text(monthSumRate + "%");
            }
            else if (request == "getNowMonthUserNums"){
                $('#monthTypeName').text(monthTypeName);
                $('#monthSum').text(data.data.nowMonthUserNums);
                var monthSumRate = Math.round((data.data.nowMonthUserNums - data.data.lastMonthUserNums) / data.data.lastMonthUserNums * 100);
                $('#monthSumRate').text(monthSumRate + "%");
            }
        }
    });
}

function getNums() {
    var data = new Date();
    var fullYear = data.getFullYear();
    var month = data.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
    var nextDate = new Date(fullYear, month, 1)
    $('#beforeUserToday').text(getBeforeUser(data.toLocaleDateString() + ' 00:00:00', new Date(data.getTime() + 24 * 60 * 60 * 1000).toLocaleDateString() + ' 00:00:00'));
    $('#beforeUserYesterDay').text(getBeforeUser(new Date(data.getTime() - 24 * 60 * 60 * 1000).toLocaleDateString() + ' 00:00:00', data.toLocaleDateString() + ' 00:00:00'));
    $('#beforeUserThisMount').text(getBeforeUser(getChangeDate(data.setDate(1)), getChangeDate(nextDate)));
    $('#beforeUserCount').text(getBeforeUser('', ''));
}

function getPendingTransactions() {
    $.ajax({
        url:'/systemIndex/getSystemIndexNums',
        method:'post',
        data:{},
        dataType:'json',
        success: function (item) {
            data = item.data;
            console.log(data);
            $("#product_to_examine").text(data.commodityNoNums);
            $("#course_to_examine").text(data.courseNoNums);
            $("#microCircle_to_examine").text(data.microCircleNoNums);
            $("#returnOrderCount").text(data.returnRequest);
            $("#productCount").text(data.commodityNums);
            $("#videoCount").text(data.courseNums);
            $("#microCircleCount").text(data.microCircleNums);
            $("#orderCount").text(data.completedOrder);
            $("#courseGoOnline").text(data.courseNums);
            $("#productGoOnline").text(data.commodityNums);
        }
    })
}

function getBeforeUser(startDate, endDate) {
    var nums = 0;
    $.ajax({
        url: '/systemIndex/getBeforeUserNums',
        type: 'post',
        async: false,
        dataType:'json',
        data: {
            'startDate': startDate,
            'endDate': endDate
        },
        success:function (data) {
            nums = data.data;
        }
    });
    return nums;
};

//日期补零
function getChangeDate(date) {
    var D, y, m, d;
    if (date) {
        D = new Date(date);
        y = D.getFullYear();
        m = D.getMonth() + 1;
        d = D.getDate();
    }
    m = m > 9 ? m : '0' + m;
    d = d > 9 ? d : '0' + d;
    return y + '-' + m + '-' + d + ' 00:00:00';
}

function getDataEcharts(request, name,typename) {
    $.ajax({
        url: '/systemIndex/' + request,
        type: 'post',
        dataType:'json',
        data: {},
        success:function (data) {
            $("#statistics").text(name);
            getEcharts(data.data.data, data.data.list,typename);
        }
    });
}

//展示echarts图
function getEcharts(data, list, typename) {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('linear'));
    // 指定图表的配置项和数据
    var option = {
        tooltip: {},
        legend: {
            data: [typename],
            bottom: 10
        },
        xAxis: {
            data: list
        },
        yAxis: {},
        series: [
            {
                name: typename,
                type: 'line',
                data: data,
                // 点的颜色。
                itemStyle: {color: 'rgb(13, 171, 158)'},
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}

function load() {
    location.reload();
}

function jump() {

}